package org.apache.dubbo.demo.provider;

import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.demo.MyDemoService;
import org.apache.dubbo.dto.MyDemoDto;
import org.apache.dubbo.rpc.Protocol;
import org.apache.dubbo.rpc.RpcContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Time;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class MyDemoServiceImpl implements MyDemoService {

    private static final Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class);

    @Override
    public String testMethod(String arg) {
        logger.info("test: " + arg + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
        return "test: " + arg + ", response from provider: " + RpcContext.getContext().getLocalAddress();
    }

    @Override
    public MyDemoDto testGetMyDemoDto() {
        MyDemoDto myDemoDto = new MyDemoDto();
        myDemoDto.setIntfiled(1);
        myDemoDto.setLdtField(LocalDateTime.now());
        myDemoDto.setLongFiled(2);
        myDemoDto.setShortField((short)3);
        myDemoDto.setStrField("123");
        return myDemoDto;
    }

    public static void main(String[] args) {
        List<Integer> nums = Arrays.asList(1,2,3);

        //输出：2
        nums.stream().filter(num -> num%2 == 0).forEach(System.out::println);

        nums.stream().map(num-> "idx_"+num).forEach(System.out::println);

        nums.stream().flatMap(num -> {
            List<String> data = new ArrayList<>();
            for (int i=0; i<num; i++){
                data.add("flatMap_"+num+"_"+i);
            }
            return data.stream();
        }).forEach(System.out::println);

        System.out.println("nums.min="+nums.stream().min((num1,num2) -> num1-num2).get());

        System.out.println("nums.max="+nums.stream().max((num1,num2) -> num1-num2).get());

        System.out.println("nums.size="+nums.stream().count());

        System.out.println("nums.findFirst="+nums.stream().findFirst().get());

        System.out.println("nums.findAny="+nums.stream().findAny().get());

        int result = nums.stream().findAny().get();
        System.out.println("nums.findAny2="+result);


        //控制台输出：anyMatch
        if(nums.stream().anyMatch(num -> num%2==0)){
            System.out.println("anyMatch");
        }

        //控制台输出：allMatch
        if(nums.stream().allMatch(num -> num > 0)){
            System.out.println("allMatch");
        }

        //控制台输出：noneMatch
        if(nums.stream().noneMatch(num -> num < 0)){
            System.out.println("noneMatch");
        }

    }

}